package com.wtwd.campus.service.impl;

import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespEntity;
import com.wtwd.campus.dao.UserMapper;
import com.wtwd.campus.entity.PersonalSetting;
import com.wtwd.campus.entity.SysUser;
import com.wtwd.campus.service.RedisService;
import com.wtwd.campus.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author mjy
 * @date 2021/06/08
 * @description
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisService redisService;

    @Override
    public Object checkUser(String account, String password) {
        //鉴别是否存在该用户
        SysUser sysUser = userMapper.getSysUserByAccount(account);
        if (sysUser==null){//不存在该用户
            return  new RespEntity(RespCode.USER_NOT_FOUNT);
        }
        //鉴别密码是否正确
        if (!sysUser.getPassword().equals(password)){
            return  new RespEntity(RespCode.PASSWORD_ERROR);
        }
        return sysUser;
    }

    @Override
    public int updateUserLastLoginTimeByUserId(Integer userId, long time) {
        return userMapper.updateUserLastLoginTimeByUserId(userId,time);
    }

    @Override
    public SysUser getSysUserByUserId(Integer userId) {
        return userMapper.getSysUserByUserId(userId);
    }

    @Override
    public Integer addOrModifyPersonalSetting(Integer userId, String name, String logoPlace) {
        //判断该用户是否存在个性设置
        Integer i = userMapper.getPersonalSetting(userId);
        if(i==0){
            //新增一笔
            i=userMapper.addPersonalSetting(userId, name, logoPlace);
        }else{
            //修改数据
            i=userMapper.modifyPersonalSetting(userId, name, logoPlace);
        }
        return i;
    }

    @Override
    public PersonalSetting getPersonalSetting(Integer userId) {
        PersonalSetting personalSetting = userMapper.getOnePersonalSetting(userId);
        return  personalSetting;
    }

    @Override
    public ArrayList<Integer> getSysUserIdListByOrgCode(String orgCode) {
        return userMapper.getSysUserIdListByOrgCode(orgCode);
    }

}
